import {getAction, putAction} from "@api/manage";

export const ConfigDictMixins = {
  data() {
    return {
      labelCol: {span: 6},
      wrapperCol: {span: 18},
      confirmSpin: false, //加载spin
      visible: false, //是否显示
      type: "", //字典索引
      disable: false, //修改或详情查询
      configData: null, //索引值对应的内容
      url: {
        queryByType: "/cms/config/queryByType",
        updateByType: "/cms/config/updateByType",
      },
    }
  },
  methods: {
    validatePass(rules, value, callback, msg) {
      if (value != msg) {
        callback(new Error('该输入框只能填写' + msg));
      } else {
        callback();
      }
    },
    resetForm() {
      this.queryJsonDict();
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.confirmSpin = true;
          console.log("提交表单信息", this.configData);
          putAction(this.url.configUpdate + "/" + this.type, this.configData).then(res => {
            if (res.success) {
              this.showSuccess(res.message);
            } else {
              this.showError(res.message);
            }
          }).finally(() => {
            this.confirmSpin = false;
          });
        } else {
          this.showWarn("请按照要求输入");
        }
      });
    },
    choseChange(e) {
      putAction(this.url.switchType, {type: this.type, params: this.configData}).then(res => {
        if (res.code === 200) {
          this.showSuccess(res.message);
        } else {
          this.showError(res.message);
          this.queryDict();
        }
      })
    },
    queryJsonDict: function () {
      this.confirmSpin = true;
      getAction(this.url.queryByType, {type: this.type}).then(res => {
        if (res.code === 200) {
          this.configData = JSON.parse(res.result);
          this.confirmSpin = false;
          console.log(this.configData);
        } else {
          this.showError(res.message);
        }
      })
    },
    queryDict: function () {
      this.confirmSpin = true;
      getAction(this.url.queryByType, {type: this.type}).then(res => {
        if (res.code === 200) {
          this.configData = res.result;
          this.confirmSpin = false;
          console.log(this.configData);
        } else {
          this.showError(res.message);
        }
      })
    },
    updateJsonDict() {
      let params = JSON.stringify(this.configData);
      this.confirmSpin = true;
      putAction(this.url.updateByType, {type: this.type, params: params}).then(res => {
        if (res.code === 200) {
          this.showSuccess(res.message);
        } else {
          this.showError(res.message);
        }
      }).finally(() => {
        this.confirmSpin = false;
      });
    },
    updateDict() {
      this.confirmSpin = true;
      putAction(this.url.updateByType, {type: this.type, params: this.configData}).then(res => {
        if (res.code === 200) {
          this.showSuccess(res.message);
        } else {
          this.showError(res.message);
        }
      }).finally(() => {
        this.confirmSpin = false;
      });
    },
    jump(url){
      window.open(url, '_blank');
    }
  }
}